<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/common.jsp"></jsp:include>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>菜单列表</title>
<script type="text/javascript">
	Ext.onReady(function(){
		Ext.QuickTips.init();
		Ext.BLANK_IMAGE_URL = basePath+"extjs/resources/images/default/s.gif";//替换图片文件地址为本地
		
		var treepanel = new Ext.tree.TreePanel({
			region:'west',
			title:'菜单效果',
			width:200,
			minSize:180,
			maxSize:250,
			autoHeight:true,
			frame:true,//美化界面
			containerScroll:false,
			rootVisible:true,
			border:false,
			animate:true
		});
		
		var rootnode = new Ext.tree.AsyncTreeNode({
			id:'0',
			text:'家电品牌总类',
			draggable:false,
			expanded:true
		});
		
		//为tree设置根节点
		treepanel.setRootNode(rootnode);
		
		//响应加载前事件，传递node参数
	    treepanel.on('beforeload', function(node) {
	        treepanel.loader.dataUrl = basePath+'home.html?menu&parentId=' + node.id; // 定义子节点的Loader
	    });
	    treepanel.on('click', treeClick);
		
	    function treeClick(node, e) {
	    	Ext.getCmp('form').getForm().load({
	    		waitMsg:'正在加载数据请稍后',
	    		waitTitle:'提示',
	    		url:basePath+'system/menu.html?edit',
	    		params:{id:node.id},
	    		method:'post',
	    		success : function(form1, action) {
	    			Ext.getCmp('form').getForm().setValues({});
	    			//Ext.getCmp('form').getForm().findField('father.id').setValue(Ext.getCmp('combo').getValue());
	    			
	    			//alert(action.result.data.father.id);
	    			//alert(action.result.data.father.text);
	    			Ext.getCmp('form').getForm().findField('father.id').setValue(action.result.data.father.id);
	    			Ext.getCmp('form').getForm().findField('father.text').setValue(action.result.data.father.text);
				},
				failure : function(form, action) {
					alert(action.result.error);
				}
	    	})
	    }
	    
	    treepanel.on('contextmenu',function(node,event){
	    	event.preventDefault();
	    	rightClick.showAt(event.getXY());
	    });
	    
		 var menuStore=new Ext.data.Store({  
			    proxy:new Ext.data.HttpProxy({url:basePath+"system/menu.html?listJson"}),  
			    reader:new Ext.data.JsonReader({root:''},  
			                                    [{name:'id'},{name: 'text'}]  
			                                  )    
			  });  
		
		var formpanel = new Ext.FormPanel({
			id:'form',
			region:'center',
			labelAlign:'left',
			buttonAlign:'center',
			bodyStyle:'padding 5px',
			width:'300px',
			height:'300px',
			frame:true,
			labelWidth:80,
			defaultType:'textfield',
			items:[
			{
				xtype:'hidden',
				name:'id'
			},{
				fieldLabel:'菜单名称',
				name:'text',
				anchor:'40%'
			},{
				fieldLabel:'路径',
				name:'url',
				anchor:'40%'
			},{
				xtype:'combo',
				fieldLabel:'叶子节点',
				name:'leaf',
				store: ['true','false'],
				model:'local',
				triggerAction:'all'
			},{
				xtype:'combo',
				fieldLabel:'是否有效',
				name:'dis',
				store:['true','false'],
				model:'local',
				triggerAction:'all'
			},{
				xtype:'numberfield',
				fieldLabel:'序号',
				name:'sort'
			},{
				xtype:'hidden',
				name:'father.id'
			},new Ext.form.ComboBox({
				  id:'combo',
			      store:menuStore,  
			      fieldLabel: '父节点',  
			      name: 'father.text',    
				  emptyText: '请选节点',  
				  mode: 'remote',  
				  triggerAction: 'all',  
				  valueField: 'id',  
				  displayField: 'text',        
				  allowBlank: true,
				  listeners:{
				       'select':function(arg){
				    	   Ext.getCmp('form').getForm().findField('father.id').setValue(Ext.getCmp('combo').getValue());
				    	   //alert(Ext.getCmp('form').getForm().findField('father.id').getValue());
				       }
				   }
			  }),{  
				xtype:'button',
				width:100,
                text:'提交',
                iconCls:'save',
                handler:function(){
                	Ext.getCmp('form').getForm().submit({
                		waitMsg:'正在提交数据',
                		waitTitle:'提示',
                		url:basePath+'system/menu.html?save',
                		method:'post',
                		success:function(form,action){
                			Ext.Msg.alert('提示','保存成功');
                			Ext.getCmp('form').getForm().reset();
                		},
                		failure:function(form,action){
                			Ext.Msg.alert('提示','原因如下:'+action.result.errors);
                		}
                	});
                }
            },{
            	xtype:'button',
				text:'重置',
				width:100,
				iconCls:'cancel',
				handler:function(){
					Ext.getCmp('form').getForm().reset();
				}
            },{
            	xtype:'button',
            	text:'删除',
            	width:100,
            	iconCls:'delete',
            	handler:function(){
            		var id = Ext.getCmp('form').getForm().findField('id').getValue();
            		Ext.Msg.confirm('确认','真的删除吗?',function(btn){
            			if(btn=='yes'){
            				Ext.Ajax.request({
								url : basePath+'system/menu.html?delete',
								params:{id:id},
								success : function(result) {
									Ext.Msg.alert('提示','删除成功');
									menuStore.reload();
								}
							});
            			}
            		});
            	}
            }]
		});
		
		new Ext.Viewport({
			layout:'border',
			default:{
				activeItem:0
			},
			items:[treepanel,formpanel]
		});
	});
</script>
</head>
<body>

</body>
</html>